package com.ldoublem.loadingviewlib.view.base;


import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Paint;
import ohos.app.Context;

import static ohos.agp.animation.Animator.CurveType.LINEAR;


public abstract class LVBase extends Component implements Animator.StateChangedListener, Animator.LoopedListener {

    public static final int RESTART = 1;
    public static final int INFINITE = -1;
    public static final int REVERSE = 2;
    public LVBase(Context context) {
        this(context, null);
    }

    public LVBase(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    public LVBase(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        InitPaint();
    }

    public void startAnim() {
        stopAnim();
        startViewAnim(0f, 1f, 500);
    }

    public void startAnim(int time) {
        stopAnim();
        startViewAnim(0f, 1f, time);
    }


    public void stopAnim() {
        if (valueAnimator != null) {
            createAnimatorProperty().reset();
            valueAnimator.setLoopedCount(0);
            valueAnimator.cancel();
            valueAnimator.end();
            if (OnStopAnim() == 0) {
                valueAnimator.setLoopedCount(0);
                valueAnimator.cancel();
                valueAnimator.end();
            }

        }
    }

   public AnimatorValue valueAnimator;

    private AnimatorValue startViewAnim(float startF, final float endF, long time) {
        valueAnimator = new AnimatorValue();
        valueAnimator.setDuration(time);
        valueAnimator.setCurveType(LINEAR);
        valueAnimator.setLoopedCount(SetAnimRepeatCount());

        valueAnimator.setStateChangedListener(this);
        valueAnimator.setLoopedListener(this);
        valueAnimator.setValueUpdateListener((animatorValue, v) -> {
            OnAnimationUpdate(animatorValue,v);
        });


        if (!valueAnimator.isRunning()) {
            AinmIsRunning();
            valueAnimator.start();

        }

        return valueAnimator;
    }


    protected abstract void InitPaint();

    protected abstract void OnAnimationUpdate(AnimatorValue valueAnimator, float v);

    protected abstract void OnAnimationRepeat(Animator animation);

    protected abstract int OnStopAnim();

    protected abstract int SetAnimRepeatMode();

    protected abstract int SetAnimRepeatCount();

    protected abstract void AinmIsRunning();


    public int dip2px(float dpValue) {
        final float scale = AttrHelper.getDensity(getContext());
        return (int) (dpValue * scale + 0.5f);

    }

    public float getFontlength(Paint paint, String str) {

        return paint.getTextBounds(str).getWidth();

    }

    public float getFontHeight(Paint paint, String str) {
        return paint.getTextBounds(str).getHeight();

    }

    public float getFontHeight(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        return fm.descent - fm.ascent;
    }


    @Override
    public void onStart(Animator animator) {

    }

    @Override
    public void onStop(Animator animator) {

    }

    @Override
    public void onCancel(Animator animator) {

    }

    @Override
    public void onEnd(Animator animator) {

    }

    @Override
    public void onPause(Animator animator) {

    }

    @Override
    public void onResume(Animator animator) {

    }

    @Override
    public void onRepeat(Animator animator) {
        OnAnimationRepeat(animator);
    }
}
